package suites.observer {
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertTrue;
	import org.splink.library.observer.IObserver;
	import org.splink.library.observer.ISubject;
	import org.splink.library.observer.Subject;

	/**
	 * @author Max Kugland
	 */
	public class ObserverTest implements ISubject, IObserver {
		private var _subject : Subject;
		private var _completed : Array;
		private static const DATA : String = "test";
		
		[Before]
		public function before() : void {
			_completed = new Array;
			_subject = new Subject();
		}
		
		[After]
		public function after() : void {
			finalize();
			_subject = null;
			_completed = null;
		}
		
		[Test(description="subscribe and notify")]
		public function test() : void {
			subscribe(this);
			notify(DATA);
			
			assertEquals(1, _completed.length);
		}
		
		[Test(description="subscribe, unsubscribe and notify")]
		public function testUnsubscribe() : void {
			subscribe(this);
			unsubscribe(this);
			notify(DATA);
			
			assertEquals(0, _completed.length);
		}
		
		[Test(description="subscribe, finalize and notify")]
		public function testFinalize() : void {
			subscribe(this);
			finalize();
			notify(DATA);
			
			assertEquals(0, _completed.length);
		}

		public function update(subject : ISubject, data : *) : void {
			assertTrue(subject == _subject);
			assertEquals(DATA, data);
			_completed.push("");
		}

		public function subscribe(observer : IObserver) : void {
			_subject.subscribe(observer);
		}

		public function unsubscribe(observer : IObserver) : void {
			_subject.unsubscribe(observer);
		}

		public function notify(data : * = null) : void {
			_subject.notify(data);
		}

		public function finalize() : void {
			_subject.finalize();
		}
	}
}
